﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace SGD_ParticleEditor
{
    class Particle
    {
        float lifetime;
        float currLife;
        float velocityX, velocityY;
        float startScale, endScale;
        float currScale;
        float positionX, positionY;
        float accelerationX, accelerationY;
        Color startColor, endColor;
        Color currColor;

        public float VelocityY
        {
            get { return velocityY; }
            set { velocityY = value; }
        }

        public float VelocityX
        {
            get { return velocityX; }
            set { velocityX = value; }
        }

        public float AccelerationX
        {
            get { return accelerationX; }
            set { accelerationX = value; }
        }

        public float AccelerationY
        {
            get { return accelerationY; }
            set { accelerationY = value; }
        }

        public float Lifetime
        {
            get { return lifetime; }
            set { lifetime = value; }
        }

        public float CurrLife
        {
            get { return currLife; }
            set { currLife = value; }
        }

        public float StartScale
        {
            get { return startScale; }
            set { startScale = value; }
        }

        public float EndScale
        {
            get { return endScale; }
            set { endScale = value; }
        }

        public float CurrScale
        {
            get { return currScale; }
            set { currScale = value; }
        }

        public float PositionX
        {
            get { return positionX; }
            set { positionX = value; }
        }

        public float PositionY
        {
            get { return positionY; }
            set { positionY = value; }
        }

        public Color StartColor
        {
            get { return startColor; }
            set { startColor = value; }
        }

        public Color EndColor
        {
            get { return endColor; }
            set { endColor = value; }
        }

        public Color CurrColor
        {
            get { return currColor; }
            set { currColor = value; }
        }

        public Particle(float _lifeTime, float _startScale, float _endScale, float _positionX, float _positionY, 
            Color _startColor, Color _endColor, float _velocityX, float _velocityY, float _accelerationX, float _accelerationY)
        {
            // Life
            lifetime = _lifeTime;
            currLife = lifetime;

            // Movement
            velocityX = _velocityX;
            velocityY = _velocityY;
            accelerationX = _accelerationX;
            accelerationY = _accelerationY;

            // Scale
            startScale = _startScale;
            endScale = _endScale;
            currScale = startScale;

            // Position
            positionX = _positionX;
            positionY = _positionY;

            // Colors
            startColor = _startColor;
            endColor = _endColor;
            currColor = startColor;
        }

        public void Update(float _dt)
        {
            // Interpolation value
            float percentLife = currLife / lifetime;

            // Apply Acceleration
            velocityX += accelerationX * _dt;
            velocityY += accelerationY * _dt;

            // Movement
            positionX += velocityX * _dt;
            positionY += velocityY * _dt;

            // Adjust life timing
            currLife -= _dt;
            if (currLife <= 0.0f)
                currLife = 0.0f;

            // Adjust size
            currScale = (startScale - endScale) * percentLife + endScale;

            // Adjust color
            int tempA = (int)((startColor.A - endColor.A) * percentLife + endColor.A);
            int tempR = (int)((startColor.R - endColor.R) * percentLife + endColor.R);
            int tempG = (int)((startColor.G - endColor.G) * percentLife + endColor.G);
            int tempB = (int)((startColor.B - endColor.B) * percentLife + endColor.B);

            // Error Prevention
            if (tempA < 0)
                tempA = 0;
            else if (tempA > 255)
                tempA = 255;

            if (tempR < 0)
                tempR = 0;
            else if (tempR > 255)
                tempR = 255;

            if (tempG < 0)
                tempG = 0;
            else if (tempG > 255)
                tempG = 255;

            if (tempB < 0)
                tempB = 0;
            else if (tempB > 255)
                tempB = 255;

            currColor = Color.FromArgb(tempA, tempR, tempG, tempB);
        }
    }
}
